Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
75.00% |
3 / 4 |
CRAP | |
96.00% |
48 / 50 |
| UserNormalizer | |
0.00% |
0 / 1 |
|
75.00% |
3 / 4 |
11 | |
96.00% |
48 / 50 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
8 / 8 |
|||
| normalize | |
0.00% |
0 / 1 |
7.01 | |
94.87% |
37 / 39 |
|||
| supportsNormalization | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
| getRoleNames | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| <?php | |
| namespace Akeneo\UserManagement\Component\Normalizer; | |
| use Akeneo\Tool\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface; | |
| use Akeneo\UserManagement\Component\Model\Role; | |
| use Akeneo\UserManagement\Component\Model\UserInterface; | |
| use Oro\Bundle\PimDataGridBundle\Repository\DatagridViewRepositoryInterface; | |
| use Oro\Bundle\SecurityBundle\SecurityFacade; | |
| use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | |
| use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; | |
| use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |
| /** | |
| * User normalizer | |
| * | |
| * @author Filips Alpe <filips@akeneo.com> | |
| * @copyright 2015 Akeneo SAS (http://www.akeneo.com) | |
| * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
| */ | |
| class UserNormalizer implements NormalizerInterface | |
| { | |
| /** @var DateTimeNormalizer */ | |
| private $dateTimeNormalizer; | |
| /** @var NormalizerInterface */ | |
| private $fileNormalizer; | |
| /** @var SecurityFacade */ | |
| private $securityFacade; | |
| /** @var TokenStorageInterface */ | |
| private $tokenStorage; | |
| /** @var DatagridViewRepositoryInterface */ | |
| private $datagridViewRepo; | |
| /** @var array */ | |
| private $properties; | |
| /** @var array */ | |
| protected $supportedFormats = ['array', 'standard', 'internal_api']; | |
| /** @var array */ | |
| private $userNormalizers; | |
| /** | |
| * @param DateTimeNormalizer $dateTimeNormalizer | |
| * @param NormalizerInterface $fileNormalizer | |
| * @param SecurityFacade $securityFacade | |
| * @param TokenStorageInterface $tokenStorage | |
| * @param DatagridViewRepositoryInterface $datagridViewRepo | |
| * @param array $userNormalizers | |
| * @param string[] $properties | |
| */ | |
| public function __construct( | |
| DateTimeNormalizer $dateTimeNormalizer, | |
| NormalizerInterface $fileNormalizer, | |
| SecurityFacade $securityFacade, | |
| TokenStorageInterface $tokenStorage, | |
| DatagridViewRepositoryInterface $datagridViewRepo, | |
| array $userNormalizers = [], | |
| string ...$properties | |
| ) { | |
| $this->dateTimeNormalizer = $dateTimeNormalizer; | |
| $this->fileNormalizer = $fileNormalizer; | |
| $this->securityFacade = $securityFacade; | |
| $this->tokenStorage = $tokenStorage; | |
| $this->datagridViewRepo = $datagridViewRepo; | |
| $this->properties = $properties; | |
| $this->userNormalizers = $userNormalizers; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function normalize($user, $format = null, array $context = []) | |
| { | |
| $result = [ | |
| 'code' => $user->getUsername(), # Every Form Extension requires 'code' field. | |
| 'enabled' => $user->isEnabled(), | |
| 'username' => $user->getUsername(), | |
| 'email' => $user->getEmail(), | |
| 'name_prefix' => $user->getNamePrefix(), | |
| 'first_name' => $user->getFirstName(), | |
| 'middle_name' => $user->getMiddleName(), | |
| 'last_name' => $user->getLastName(), | |
| 'name_suffix' => $user->getNameSuffix(), | |
| 'phone' => $user->getPhone(), | |
| 'image' => $user->getImagePath(), | |
| 'last_login' => $user->getLastLogin() ? $user->getLastLogin()->getTimestamp() : null, | |
| 'login_count' => $user->getLoginCount(), | |
| 'catalog_default_locale' => $user->getCatalogLocale()->getCode(), | |
| 'user_default_locale' => $user->getUiLocale()->getCode(), | |
| 'catalog_default_scope' => $user->getCatalogScope()->getCode(), | |
| 'default_category_tree' => $user->getDefaultTree()->getCode(), | |
| 'email_notifications' => $user->isEmailNotifications(), | |
| 'timezone' => $user->getTimezone(), | |
| 'groups' => $user->getGroupNames(), | |
| 'roles' => $this->getRoleNames($user), | |
| 'product_grid_filters' => $user->getProductGridFilters(), | |
| 'avatar' => null === $user->getAvatar() ? [ | |
| 'filePath' => null, | |
| 'originalFilename' => null, | |
| ] : $this->fileNormalizer->normalize($user->getAvatar()), | |
| 'meta' => [ | |
| 'id' => $user->getId(), | |
| 'form' => $this->securityFacade->isGranted('pim_user_user_edit') ? 'pim-user-edit-form' : 'pim-user-show', | |
| 'image' => [ | |
| 'filePath' => null === $user->getAvatar() ? | |
| null : | |
| $this->fileNormalizer->normalize($user->getAvatar())['filePath'] | |
| ] | |
| ] | |
| ]; | |
| $types = $this->datagridViewRepo->getDatagridViewTypeByUser($user); | |
| foreach ($types as $type) { | |
| $defaultView = $user->getDefaultGridView($type['datagridAlias']); | |
| // Set default_product_grid_view, default_published_product_grid_view, etc. | |
| $result[sprintf('default_%s_view', str_replace('-', '_', $type['datagridAlias']))] | |
| = $defaultView === null ? null : $defaultView->getId(); | |
| } | |
| $normalizedProperties = array_reduce($this->properties, function ($result, string $propertyName) use ($user) { | |
| return $result + [$propertyName => $user->getProperty($propertyName)]; | |
| }, []); | |
| $normalizedCompound = array_map(function ($normalizer) use ($user, $format, $context) { | |
| return $normalizer->normalize($user, $format, $context); | |
| }, $this->userNormalizers); | |
| $result['properties'] = $normalizedProperties; | |
| return array_merge_recursive($result, ...$normalizedCompound); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function supportsNormalization($data, $format = null) | |
| { | |
| return $data instanceof UserInterface && in_array($format, $this->supportedFormats); | |
| } | |
| /** | |
| * @param UserInterface $user | |
| * | |
| * @return string[] | |
| */ | |
| private function getRoleNames(UserInterface $user): array | |
| { | |
| return $user->getRolesCollection()->map(function (Role $role) { | |
| return $role->getRole(); | |
| })->toArray(); | |
| } | |
| } |